package com.kzd.gtgame.main.mine

import android.view.View
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.FileUtils
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory
import com.kzd.gtgame.R
import com.kzd.gtgame.app.SPSConstance
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.custom.setTransitionStatus
import com.kzd.gtgame.entity.IsLogin
import com.kzd.gtgame.jetpack.vm.HomeViewModel
import com.kzd.gtgame.main.*
import com.kzd.gtgame.main.popup.AppUpDatePopup
import com.kzd.gtgame.main.popup.ProgressPopup
import com.kzd.gtgame.main.popup.SettingExitPopup
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_mine_setting.*
import org.greenrobot.eventbus.EventBus
import java.lang.StringBuilder

class MineSettingActivity : OwnerViewModeActivity<HomeViewModel>() {

    private val mGlideCachePath: String
        get() = "$cacheDir/${InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR}"

    private val mProgressPopup: ProgressPopup by lazy {
        ProgressPopup(context = this, onCancel = {}, onConfirm = {})
    }

    private val mAppUpDatePopup: AppUpDatePopup by lazy {
        AppUpDatePopup(context = this, onClick = {
            mAppUpDatePopup.dismiss()
            val fileUrl = viewModel.userAppUpdateLiveData.value
                ?.data?.data?.apk_file_url ?: return@AppUpDatePopup
            viewModel.userDownload(fileUrl) {
                mProgressPopup.setProgress(it)
            }
        })
    }

    private val mSettingExitPopup: SettingExitPopup by lazy {
        SettingExitPopup(context = this, onClick = {
            if (it.id == R.id.tvSettingExitClose) {
                SPSConstance.clearUser()
                EventBus.getDefault().post(IsLogin(isLogin = false))
            } else if (it.id == R.id.tvSettingExitSwitch) {
                toAccessActivity(AccessAction.LOGIN_CUT)
            }
            finish()
        })
    }

    override fun viewModel(): HomeViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_setting

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.white, view = tlMineSettingBar)
    }

    override fun initView() {
        tvMineSettingCacheSize.text = FileUtils.getSize(mGlideCachePath)
//        FileUtils.deleteAllInDir(cacheDir)
//        tvMineSettingCacheSize.text = FileUtils.getSize(mGlideCachePath)

        //个人中心
        tvMineSettingCenter.setOnClickAntiShakeListener{
            toMineUserActivity()
        }

        //投诉反馈
        tvMineSettingFeedback.setOnClickAntiShakeListener {
            toMineFeedbackActivity()
        }

        //免责声明
        tvMineSettingState.setOnClickAntiShakeListener {
            toMineRichTextActivityApi(
                RichTextApi.AGREEMENT_MZSM.title,
                RichTextApi.AGREEMENT_MZSM.type
            )
        }

        //用户协议
        tvMineSettingUserAgree.setOnClickAntiShakeListener {
            toMineRichTextActivityApi(
                RichTextApi.AGREEMENT_YHXY.title,
                RichTextApi.AGREEMENT_YHXY.type
            )
        }

        //隐私政策
        tvMineSettingPrivacy.setOnClickAntiShakeListener {
            toMineRichTextActivityApi(
                RichTextApi.AGREEMENT_YSZC.title,
                RichTextApi.AGREEMENT_YSZC.type
            )
        }

        //检查更新
        flMineSettingUpdate.setOnClickAntiShakeListener {
            viewModel.userAppUpDate()
        }

        tvMineSettingVersionName.text = StringBuilder().append("V")
            .append(AppUtils.getAppVersionName())

        tvMineSettingExit.visibility = if (SPSConstance.uid.isNullOrEmpty())
            View.GONE else View.VISIBLE

        tvMineSettingExit.setOnClickAntiShakeListener {
            mSettingExitPopup.showPopupWindow()
        }
    }

    override fun initViewMode() {
        viewModel.userAppUpdateLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            mAppUpDatePopup.setContent(data.update_log)
                .showPopupWindow()
        })
        viewModel.userDownloadLiveData.observe(this as LifecycleOwner, { response ->
            when (response.status) {
                Status.START -> {
                    mProgressPopup.resetProgress()
                    mProgressPopup.showPopupWindow()
                }
                Status.FAILURE -> {
                    mProgressPopup.dismiss()
                    toast(response.error?.msg)
                }
                Status.SUCCESS -> {
                    mProgressPopup.dismiss()
                    val data = response.data?.data
                        ?: return@observe
                    AppUtils.installApp(data)
                }
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mSettingExitPopup.isShowing) {
            mSettingExitPopup.dismiss()
        }
    }
}